package com.rzjm.exercise.module.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.DateUtils;
import com.rzjm.exercise.common.utils.ExcelUtils;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.common.utils.StringUtils;
import com.rzjm.exercise.module.domain.*;
import com.rzjm.exercise.module.service.CarWeeksService;
import com.rzjm.exercise.module.service.PickUpRecordService;
import com.rzjm.exercise.module.service.ShopService;
import com.rzjm.exercise.module.service.ShopTypeRelationService;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Controller
@RequestMapping(value = "/module/carWeeks",produces ="application/json;charset=UTF-8")
public class CarWeeksController extends BaseController {
    @Resource
    private CarWeeksService carWeeksService;
    @Resource
    private ShopService shopService;
    @Resource
    private ShopTypeRelationService shopTypeRelationService;

    @Log("查询车辆预约星期页面")
    @GetMapping("")
    @RequiresPermissions("module:carWeeks:carWeeks")
    String carWeeks(Model model) {
        UserDO user = getUser();
        QueryWrapper<ShopTypeRelation> shopTypeRelationQw = new QueryWrapper<>();
        shopTypeRelationQw.eq("shop_type", 2);
        shopTypeRelationQw.eq("state", 1);
        List<ShopTypeRelation> shopTypeRelationList = shopTypeRelationService.list(shopTypeRelationQw);
        List<Integer> shopIds = shopTypeRelationList.stream().map(r -> r.getShopId()).collect(Collectors.toList());
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        wrapper.eq("state", 1);
        wrapper.in("id", shopIds);
        if (user.getUserType() != 1 && user.getUserType() != 0){
            wrapper.eq("id",user.getShopId());
        }
//        if(user.getUserType() == 2){//集团管理员
//            wrapper.eq("group_id",user.getGroupId());
//        }else if(user.getUserType() == 3){//片区管理员
//            wrapper.eq("pianqu_id",user.getPianquId());
//        }else if(user.getUserType() == 4){//分厂管理员
//            wrapper.eq("shop_id",user.getShopId());
//        } else if(user.getUserType() == 10){//企业管理员
//            wrapper.eq("shop_id",user.getShopId());
//        }
        List<Shop> shopList = shopService.list(wrapper);
        model.addAttribute("shopList",shopList);
        return "module/carweeks/list";
    }


    @Log("查询车辆预约星期")
    @ResponseBody
    @GetMapping("/list")
    public DataTableLayUI<CarWeeks> list(DataTableLayUI<CarWeeks> condition) {
        UserDO user = getUser();
        Map<String, Object> searchParams = condition.getSearchParams();


        String shopId = searchParams.containsKey("shopId") ? searchParams.get("shopId").toString() : null;
        String keywords = searchParams.containsKey("search_like_realName") ? searchParams.get("search_like_realName").toString() : null;

        UserDO userDO = getUser();
        if (userDO.getUserType() != 1 && userDO.getUserType() != 0 && (userDO.getShopId() == null || userDO.getShopId() == 0)){
            return new DataTableLayUI<CarWeeks>();
        }
        QueryWrapper<CarWeeks> qw = new QueryWrapper<>();
        QueryWrapper<ShopTypeRelation> shopTypeRelationQw = new QueryWrapper<>();
        shopTypeRelationQw.eq("shop_type", 2);
        shopTypeRelationQw.eq("state", 1);
        List<ShopTypeRelation> shopTypeRelationList = shopTypeRelationService.list(shopTypeRelationQw);
        List<Integer> shopIds = shopTypeRelationList.stream().map(r -> r.getShopId()).collect(Collectors.toList());
        qw.in("shop_id", shopIds);
        if (user.getUserType() != 1 && user.getUserType() != 0){
            qw.eq("shop_id",user.getShopId());
        }
//        if(userDO.getUserType() == 2){//集团管理员
//            qw.eq("group_id",userDO.getGroupId());
//        }else if(userDO.getUserType() == 3){//片区管理员
//            qw.eq("pianqu_id",userDO.getPianquId());
//        }else if(userDO.getUserType() == 4){//分厂管理员
//            qw.eq("shop_id",userDO.getShopId());
//        } else if(userDO.getUserType() == 10){//企业管理员
//            qw.eq("shop_id",userDO.getShopId());
//        }
        if(StrUtil.isNotEmpty(shopId)){
            searchParams.put("shopId", shopId);
            qw.eq("shop_id", shopId);
        }
        condition.setQw(qw);
        DataTableLayUI<CarWeeks> dt = carWeeksService.pageSearchLayUI(condition);
        List<CarWeeks> list = dt.getData();
        for (CarWeeks carWeeks : list){
            Shop shop = shopService.findById(carWeeks.getShopId());
            if (shop != null){
                carWeeks.setShopName(shop.getShopName());
            }
        }
        return dt;
    }


    @Log("新增车辆预约星期页面")
    @GetMapping("/add")
    @RequiresPermissions("module:carWeeks:add")
    String add(Model model) {
        CarWeeks carWeeks = new CarWeeks();
        model.addAttribute("carWeeks", carWeeks);
        return "module/carweeks/detail";
    }

    @Log("编辑车辆预约星期页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:carWeeks:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        CarWeeks carWeeks = carWeeksService.findById(id);
        Shop shop = shopService.findById(carWeeks.getShopId());
        if (shop != null){
            carWeeks.setShopName(shop.getShopName());
        }
        model.addAttribute("carWeeks", carWeeks);
        return "module/carweeks/detail";
    }

    @Log("新增车辆预约星期")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("module:carWeeks:add")
    public R save(@Valid CarWeeks carWeeks) {
        UserDO user = getUser();

        return R.ok();
    }

    /**
     * 修改
     */
    @Log("更新车辆预约星期")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:carWeeks:edit")
    public R update(@Valid CarWeeks carWeeks) {
        carWeeksService.updateById(carWeeks);
        return R.ok();
    }
}
